package com.btkjgc.service.impl;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AdminUser;
import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.Dictionary;
import com.btkjgc.models.ElectricChargingSite;
import com.btkjgc.models.extend.ElectricChargingSiteModel;
import com.btkjgc.service.intf.ElectricChargingSiteManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

/**
 * Created by _ESE on 2015/2/9.
 */
@Component
public class ElectricChargingSiteManageService_Impl extends BaseService_Impl
		implements ElectricChargingSiteManageService_Intf {

	static Logger logger = Logger.getLogger(ElectricChargingSiteManageService_Impl.class);

	@SuppressWarnings("unchecked")
	@Override
	public AjaxObj insertElectricChargingSiteModel(
			ElectricChargingSiteModel electricChargingSiteModel) {
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		ElectricChargingSite electricChargingSite = new ElectricChargingSite();

		try {
			PropertyUtils.copyProperties(electricChargingSite,
					electricChargingSiteModel);
			electricChargingSite.setDel(0);
			electricChargingSite.setState(1);
			electricChargingSite.setCreatedate(new Date());
			HibernateUtil.save(electricChargingSite);

		} catch (Exception e) {
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("添加时出错");
		}
		
		return ajaxObj;
	}

	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv,
			int pageIndex, int totalRecord,
			List<ElectricChargingSiteModel> electricChargingSiteList) {
		// TODO Auto-generated method stub

		AdminUser adminUser = (AdminUser) request.getSession().getAttribute(
				"user");

		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}

		mv.addObject("user_info", adminUser);
		mv.addObject("electric_charging_site_list", electricChargingSiteList);

		String sql = " select pct.name value,concat(pct.id,'') 'key'  from property_company_t pct where pct.del = 0 ";

		List<Dictionary> companyList = HibernateUtil.executeSQLQueryList(sql,
				Dictionary.class);
		if (companyList.size() > 0) {
			mv.addObject("companyList", companyList);
		}

		sql = " select * from dictionary_t dt where dt.name = 'SiteState' ";

		List<Dictionary> stateList = HibernateUtil.executeSQLQueryList(sql,
				Dictionary.class);
		if (companyList.size() > 0) {
			mv.addObject("stateList", stateList);
		}

		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
	}

	@Override
	public AjaxObj updateElectricChargingSiteModel(ElectricChargingSite  electricChargingSite) {
		// TODO Auto-generated method stub
		AjaxObj ajaxObj = new AjaxObj();
		ajaxObj.setResult(true);
	 
		try {
			HibernateUtil.executeUpdate(getUpdateHql(electricChargingSite));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("更新时发生错误");

		}

		return ajaxObj;
	}

}
